package com.dycx.payment.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.common.SerialNumberUtil;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.payment.converter.PaymentOrderConverter;
import com.dycx.payment.dto.PaymentOrderDTO;
import com.dycx.payment.entity.PaymentOrderDO;
import com.dycx.payment.mapper.PaymentOrderMapper;
import com.dycx.payment.service.PaymentOrderService;
import com.dycx.payment.vo.PaymentOrderVO;
import com.dycx.system.entity.LogDO;
import com.dycx.system.service.ConfigService;
import com.dycx.system.service.LogService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 支付订单表服务实现类
 *
 * @author wuzongbin
 * @since 2024-01-16 09:57:09
 */

@Service
@AllArgsConstructor
@Slf4j
public class PaymentOrderImpl extends ServiceImpl<PaymentOrderMapper, PaymentOrderDO> implements PaymentOrderService {

    private LogService logService;
    private ConfigService configService;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<PaymentOrderVO> getPage(PaymentOrderDTO dto) {
        LambdaQueryWrapper<PaymentOrderDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(PaymentOrderDO::getId);

        Page<PaymentOrderDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        Page<PaymentOrderVO> voPage = PaymentOrderConverter.entityToVoPage(entityPage);
        voPage.getRecords().forEach(this::setData);

        return voPage;
    }

    /**
     * 查询详情
     *
     * @param payOrderId 主键
     * @return 查询结果
     */
    @Override
    public PaymentOrderVO load(Integer payOrderId) {
        PaymentOrderDO entity = getById(payOrderId);

        return setData(PaymentOrderConverter.entityToVo(entity));
    }

    /**
     * 设置数据
     *
     * @param vo 实体
     * @return 查询结果
     */
    public PaymentOrderVO setData(PaymentOrderVO vo) {
        return vo;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public String create(PaymentOrderDTO dto) {
        PaymentOrderDO entity = BeanUtil.toBean(dto, PaymentOrderDO.class);
        entity.setOutTradeNo(SerialNumberUtil.createPaymentOrderNo());
        entity.setResult(false);
        save(entity);

        LogDO logDO = new LogDO();
        logDO.setPost(JSON.toJSONString(dto));
        logDO.setBizType(BizTypeEnum.PAY.getCode());
        logDO.setBizValue(dto.getBizOrderNo());
        logDO.setRoute("发起支付");
        logDO.setResult(true);

        logService.save(logDO);

        return entity.getOutTradeNo();
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(PaymentOrderDTO dto) {
        PaymentOrderDO entity = BeanUtil.toBean(dto, PaymentOrderDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param payOrderId 主键
     * @return
     */
    @Override
    public Boolean delete(Integer payOrderId) {
        return removeById(payOrderId);
    }

}

